package com.study.javaOther.bishi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @ClassName: TreeDemo
 * @Description:
 * @author: 王胜
 * @date 2021/6/21
 * @Version: 1.0
 **/
public class TreeDemo {
    static List<FileNode> list = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        TreeNode treeNode = null;
        try {
            treeNode = loadFile(new File("D:\\WorkSpace\\GitHub\\javaee_learning_code\\java_code\\src\\main\\java\\com\\study\\bishi\\treedemo.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        SearchDemo.breadthFirstSearch(treeNode);
    }

    private static TreeNode loadFile(File txtFile) throws Exception {
        FileReader fr = new FileReader(txtFile);
        BufferedReader br = new BufferedReader(fr);
        String line = new String();
        while ((line = br.readLine()) != null) {
            String[] split = line.split("\\|");
            list.add(new FileNode(Integer.parseInt(split[0]), split[1]));
        }
        br.close();
        fr.close();
        TreeNode treeNode = createTree(list);
        return treeNode;
    }

    private static TreeNode createTree(List<FileNode> list) {
        TreeNode root = null;
        TreeNode popNode = null;
        Integer rootLevel = 0;
        if (list == null || list.size() <= 0) {
            return root;
        }
        Stack<FileNode> stack = new Stack<>();
        stack.push(list.get(0));
        for (int i = 1; i < list.size(); i++) {
            Integer curLevel = list.get(i).getLevel();
            if (stack.peek().getLevel() <= curLevel) {
                stack.push(list.get(i));
            } else {
                while (stack.peek().getLevel() > curLevel || rootLevel != curLevel) {
                    FileNode pop = stack.pop();
                    popNode = pop.getNode();
                    if (root != null && root.getNodeName() == null) {
                        for (TreeNode childrenNode : root.getChildrenNodes()) {
                            popNode.addChildren(childrenNode);
                        }
                    } else {
                        popNode.addChildren(root);
                    }
                    root = popNode;
                    rootLevel = pop.getLevel();
                }
                while (stack.peek().getLevel() == curLevel) {
                    FileNode pop = stack.pop();
                    TreeNode temp = new TreeNode();
                    temp.addChildren(pop.getNode());
                    if (root != null && root.getNodeName() == null) {
                        for (TreeNode childrenNode : root.getChildrenNodes()) {
                            temp.addChildren(childrenNode);
                        }
                    } else {
                        temp.addChildren(root);
                    }
                    root = temp;
                }
                stack.push(list.get(i));
            }
        }

        while (!stack.isEmpty()) {
            FileNode pop = stack.pop();
            popNode = pop.getNode();
            popNode.addChildren(root);
            root = popNode;
        }
        return root;
    }

}
